home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1994…tember: Reference Library / Dev.CD Sep 94.toast / Periodicals / develop / develop Issue 11 / develop 11 code / MultiBuffer / MultiBuffer Source / oscilator.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-07-15  |  2.4 KB  |  83 lines  |  [TEXT/MPS ]

  1. /*
  2. 1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890
  3. |         |         |         |         |         |         |         |         |         |         
  4.  
  5. */
  6.  
  7. /*
  8. **        Oscilator.c
  9. **
  10. **            This file contains a simple oscilator for generating tones to pass off to the Sampled
  11. **            Sound synthisizer...
  12. **
  13. **            (NMD)    1/19/92        created
  14. **
  15. */
  16.  
  17. #pragma load "MacHeaders"
  18.  
  19.  
  20.  
  21. #ifndef __MAININCLUDES__
  22. #include "MainApp.h"
  23. #endif
  24.  
  25. #include <SANE.h>
  26. #include <Math.h>
  27.  
  28. #define kSampleRate            22000                                            // 22kHz Sample rate
  29.  
  30. #define Long2X(x)            Fix2X(Long2Fix(x))
  31. #define    X2Long(x)            Fix2Long(X2Fix(x))
  32.  
  33. WaveCyclePtr NewWaveForm (unsigned char amplitude, unsigned short frequency)
  34. {
  35.     WaveCyclePtr            wave                        = nil;                // return structure
  36.     unsigned short             cycleLength;                                    // # of samples in cycle
  37.     unsigned short            sampleIterator;                                    // iterate accross sample
  38.     Ptr                     sampleArea                     = nil;                // space to hold sample
  39.  
  40.     extended                appxPI;                                            // π approximation
  41.     extended                samplePt;                                        // one sample point
  42.     extended                xAmp;                                            // exteneded amplitude
  43.     extended                xOffset;                                        // 0x80 in extended form
  44.     extended                inc;                                            // radians/ iteration
  45.     
  46.     wave = (WaveCyclePtr) NewPtrClear (sizeof(WaveCycle));
  47.     
  48.     if (wave) {
  49.         //    First order of business: figure out how many samples there are in one cycle of a
  50.         //    wave at the given frequency at our current sample rate.  the formula is as follows:
  51.         //    
  52.         //            (n Samples/Cycle) = (x Samples/Second) / (y Cycles/Second)
  53.         cycleLength = kSampleRate / frequency;
  54.         
  55.         //    Allocate space for new sample buffer
  56.         sampleArea = NewPtrClear (cycleLength);
  57.         Assert (sampleArea == nil, "\pCouldn't allocate space for the sample area");
  58.         if (sampleArea != nil) {
  59.             
  60.             //    Retrieve SANE's approximation of pi
  61.             appxPI = pi();
  62.             
  63.             xOffset = (extended) 0x00000080;
  64.             
  65.             //    Figure out how many radians apart each sample point is
  66.             inc = (2*appxPI) / (extended)cycleLength;
  67.             
  68.             //    convert the Amplitude to type extened so we can use it in the trig calculation
  69.             xAmp = (extended)amplitude;
  70.             
  71.             for (sampleIterator = 0; sampleIterator < cycleLength; sampleIterator++) {
  72.                 samplePt = xAmp * sin (inc * (extended)sampleIterator) + xOffset;
  73.                 *(sampleArea + sampleIterator) = (long)samplePt;
  74.             }
  75.         }
  76.     }
  77.     
  78.     wave->length = cycleLength;
  79.     wave->wavePtr = sampleArea;
  80.  
  81.     return (wave);
  82. }
  83.